package com.boomsecret.utils;

import java.util.regex.Pattern;

/**
 * Created by wangjinliang on 2018/1/5.
 */
public final class PatternUtils {

    public static final String EMAIL_REGEX = "(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)";  // 邮箱正则表达式
    public static final String USERNAME_REGEX = "[a-zA-Z]\\w{5,14}"; // 用户名6~15位
    public static final String CHINESE_REGEX = "[\u4e00-\u9fa5]*";   // 验证是否是汉字
    public static final String PHONE_REGEX = "1[0-9]{10}";   // 简单的手机号码验证
    public static final String URL_REGEX = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";  // URL
    public static final String ID_CARD_REGEX = "(\\d{15})|(\\d{17}(?:\\d|x|X))";

    private PatternUtils() {}
    
    public static boolean isEmail(String email) {
        return Pattern.matches(EMAIL_REGEX, email);
    }

    public static boolean isUserName(String username) {
        return Pattern.matches(USERNAME_REGEX, username);
    }

    public static boolean isChinese(String chinese) {
        return Pattern.matches(CHINESE_REGEX, chinese);
    }

    public static boolean isPhone(String phone) {
        return Pattern.matches(PHONE_REGEX, phone);
    }

    public static boolean isUrl(String url) {
        return Pattern.matches(URL_REGEX, url);
    }

    public static boolean isIdCard(String idCard) {
        return Pattern.matches(ID_CARD_REGEX, idCard);
    }

    public static void main(String[] args) {
        System.out.println(isEmail("18201225281@163.com"));
        System.out.println(isUserName("w182011gg"));
        System.out.println(isChinese("恢复哇"));
        System.out.println(isPhone("18201225281"));
        System.out.println(isUrl("https://www.jd.com"));
        System.out.println(isUrl("http://www.jd.com"));
    }
}
